package com.wwwarehouse.contract.contract.publishgoods;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;

import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.contract.adapter.publishgoods.SettingRulesAdapter;
import com.wwwarehouse.contract.bean.ResourceItemBean;
import com.wwwarehouse.contract.bean.SettingRuleBean;
import com.wwwarehouse.contract.contract.TheParentFragment;
import com.wwwarehouse.contract.event.RefreshEvent;

import java.util.ArrayList;
import java.util.List;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * @author Joseph on 2017/10/19.
 *         <p/>
 *         商品设置规则
 */

public class SettingRulesGoodsListFragment extends TheParentFragment {

    private RecyclerView mRvContent;
    private String pbUkid;
    SettingRulesAdapter mAdapter;
    private int num;
    private int pos;
    private String sort;
    private List<SettingRuleBean.DataBean.ListBean> settingRuleData;
    private List<SettingRuleBean.DataBean.ListBean> mList;
    private List<ResourceItemBean.DataBean.ListBean> selectList;

    @Override
    protected int getContentId() {
        return R.layout.release_object_set_rule_list;
    }

    @Override
    protected void init(View view) {
        mRvContent = findView(view, R.id.rv_content);
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    @Override
    protected void loadDatas() {
        Bundle b = getArguments();
        if (b == null)
            return;
        pbUkid = b.getString("pbUkid");
        pos = b.getInt("pos")-1;
        num = b.getInt("num");
        sort = b.getString("sort");
        settingRuleData = (List<SettingRuleBean.DataBean.ListBean>) b.getSerializable("settingRuleData");
        selectList = (List<ResourceItemBean.DataBean.ListBean>) b.getSerializable("selectList");
        mList = new ArrayList<>();
        if (settingRuleData!=null){
            for (int i = pos * num; i < num + pos * num; i++) {
                if (i >= 0 && i < settingRuleData.size()) {
                    mList.add(settingRuleData.get(i));
                }
            }
        }


        mRvContent.setLayoutManager(new GridLayoutManager(mActivity, 1) {
            @Override
            public boolean canScrollHorizontally() {
                return false;
            }

            @Override
            public boolean canScrollVertically() {
                return false;
            }

        });
        final ViewTreeObserver observer = mRvContent.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                observer.removeGlobalOnLayoutListener(this);
                if (mAdapter == null) {
                    mAdapter = new SettingRulesAdapter(mActivity, mList, mRvContent.getHeight(), mRvContent.getWidth(), num, pos, pbUkid);
                }
                mRvContent.setAdapter(mAdapter);
            }
        });



        EventBus.getDefault().register(this);


    }

    public void onEventMainThread(Object event) {
        if (event instanceof RefreshEvent) {
            if (pos == ((RefreshEvent) event).getPage()) {
                mList.get(((RefreshEvent) event).getPosition()).setHasSetRule(true);
                mList.get(((RefreshEvent) event).getPosition()).setPriceRuleType(((RefreshEvent) event).getBean().getPriceRuleType());
                mList.get(((RefreshEvent) event).getPosition()).setPriceRuleValue(((RefreshEvent) event).getBean().getPriceRuleValue());
                mList.get(((RefreshEvent) event).getPosition()).setQtyRuleType(((RefreshEvent) event).getBean().getQtyRuleType());
                mList.get(((RefreshEvent) event).getPosition()).setQtyRuleValue(((RefreshEvent) event).getBean().getQtyRuleValue());
                mList.get(((RefreshEvent) event).getPosition()).setPrice(((RefreshEvent) event).getBean().getPrice());
                mList.get(((RefreshEvent) event).getPosition()).setQty(((RefreshEvent) event).getBean().getQty());
                mList.get(((RefreshEvent) event).getPosition()).setUnit(((RefreshEvent) event).getBean().getUnit());
                mList.get(((RefreshEvent) event).getPosition()).setDefinedUkid(((RefreshEvent) event).getBean().getDefinedUkid());
                mList.get(((RefreshEvent) event).getPosition()).setPbUkid(((RefreshEvent) event).getBean().getPbUkid());
                mList.get(((RefreshEvent) event).getPosition()).setPbPrice(((RefreshEvent) event).getBean().getPbPrice());
                mList.get(((RefreshEvent) event).getPosition()).setPbQty(((RefreshEvent) event).getBean().getPbQty());
                mList.get(((RefreshEvent) event).getPosition()).setPbQtyUnit(((RefreshEvent) event).getBean().getPbQtyUnit());
                mList.get(((RefreshEvent) event).getPosition()).setPbQtyUnitUkid(((RefreshEvent) event).getBean().getPbQtyUnitUkid());
                mAdapter.notifyDataSetChanged();
            }
        }
    }

    public List<SettingRuleBean.DataBean.ListBean> getSettingrulesData(){
        return mList;
    }


    @Override
    public void onSuccess(CommonClass commonClass, int what) {

    }

    @Override
    public void onReLoad(int code) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        if (mAdapter != null) {
            mAdapter.dissPop();
        }
    }
}
